//
//  UpdPassViewController.m
//  Dear
//
//  Created by n22 on 15/7/16.
//  Copyright (c) 2015年 陈俊俊. All rights reserved.
//

#import "UpdPassViewController.h"
#define OffsetY 10
#define OriginY 20
#define LeftMargin 20
#define LeftWidth 80
#define SendWidth 80
#define SendHeight 40
#define TextFieldHeight 44
#define TotalHeight 132
#define ImageWidth 15
#define ImageHeight 20
#define TopMargin 10
#define TopHeight 11

@interface UpdPassViewController ()<UITextFieldDelegate>
{
     UIView *_bgView;
     UIButton *_registerBtn;
}
@property (weak, nonatomic)  NSLayoutConstraint *topLabelH;
@property (weak, nonatomic)  UITextField *oldTextFiled;

@property (weak, nonatomic)  UITextField *otherTextFiled;
@property (weak, nonatomic)  UITextField *againTextField;
@property (assign,nonatomic) CGFloat keyBoardHeight;//记录键盘的高度
@property (assign,nonatomic) CGFloat keyBoardTime;//记录弹键盘的时间
@property (assign,nonatomic) BOOL isagain;
@end

@implementation UpdPassViewController

- (instancetype)init
{
    self =  [super init];
    if (self) {
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeKeyBoard:) name:UIKeyboardDidChangeFrameNotification object:nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTitleViewWith:@"修改密码"];
    self.view.backgroundColor = RGBA(246, 246, 246, 1);
    [self bulidLayout];
}
#pragma mark - 创建视图
- (void)bulidLayout
{
    _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenSize.width, ScreenSize.height)];
    [self.view addSubview:_bgView];
 
    
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, TopMargin, ScreenSize.width, TextFieldHeight*3)];
    topView.backgroundColor = [UIColor whiteColor];
    [_bgView addSubview:topView];
    
    //创建文本框
    NSArray *imagearr = @[@"login4",@"login4",@"login5"];
    NSArray *tipsArr = @[@"请输入旧密码",@"请输入新密码",@"请再次输入确认密码"];
    for (NSInteger i = 0; i < 3; i++) {
        UIImageView *oneImage = [self createImageView:imagearr[i]];
        oneImage.contentMode = UIViewContentModeScaleAspectFit;
        oneImage.frame = CGRectMake(LeftMargin, TopHeight+TextFieldHeight*i, ImageWidth, ImageHeight);
        [topView addSubview:oneImage];
        UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(oneImage.frame) + LeftMargin, TextFieldHeight*i, CGRectGetWidth(topView.frame) - LeftMargin * 3 - ImageWidth, TextFieldHeight)];
        textFiled.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
        textFiled.textAlignment = NSTextAlignmentLeft;
        textFiled.backgroundColor = [UIColor clearColor];
        textFiled.delegate = self;
        textFiled.placeholder = tipsArr[i];
        textFiled.font = FontSize(15);
        textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
        [topView addSubview:textFiled];
        textFiled.secureTextEntry = YES;
        switch (i) {
            case 0:
            {
                self.oldTextFiled = textFiled;
            
            }
                break;
            case 1:
            {
                self.otherTextFiled = textFiled;
                
            }
                break;
            case 2:
            {
                
                self.againTextField = textFiled;
            }
                break;
            default:
                break;
        }
        if (i < tipsArr.count - 1) {
            UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(LeftMargin, (TextFieldHeight - 1)*(i+1), ScreenSize.width - LeftMargin*2, 1)];
            line.backgroundColor = HexColor(@"e4e4e4");
            [topView addSubview:line];
        }
    }
    
    
    //创建下一步按钮
    _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_registerBtn setTitle:@"下一步" forState:UIControlStateNormal];
    _registerBtn.frame = CGRectMake(LeftMargin, CGRectGetMaxY(topView.frame) + LeftMargin, ScreenSize.width - LeftMargin*2, TextFieldHeight);
    _registerBtn.backgroundColor =  SameColor;
    _registerBtn.layer.cornerRadius = 5;
    _registerBtn.layer.masksToBounds = YES;
    [_registerBtn addTarget:self action:@selector(updPass:) forControlEvents:UIControlEventTouchUpInside];
    _registerBtn.titleLabel.font = FontSize(15);
    [_bgView addSubview:_registerBtn];
    
}
#pragma mark -创建底部的imageView
- (UIImageView *)createImageView:(NSString *)name
{
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:name];
    return imageView;
}

#pragma mark - 修改密码
- (void)updPass:(UIButton *)btn {
    NSString *oldPass = [CJJHelper trimmingWhitespace:self.oldTextFiled.text];
    NSString *otherPass = [CJJHelper trimmingWhitespace:self.otherTextFiled.text];
    NSString *againPass = [CJJHelper trimmingWhitespace:self.againTextField.text];
    if (oldPass.length == 0 || otherPass.length == 0 || againPass.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"信息不完成请填写完整" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }else{
        if (![oldPass isEqualToString:[DearUser sharedInstance].passWord]) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"旧密码不正确" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }else if(![otherPass isEqualToString:againPass]){
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"两次密码不一致" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }else{
            //修改密码
            [self savePass:oldPass againPass:otherPass];
        }
    
    }
}

#pragma mark - 修改密码
- (void)savePass:(NSString *)otherPass againPass:(NSString *)againPass{
    
    NSDictionary *dict = @{@"userName":[DearUser sharedInstance].userName,@"password":otherPass,@"newPassword":againPass};
    NSString *url = [ServerUrl stringByAppendingFormat:@"/%@",DearHttpModifyPass];
    [[HttpRequest sharedInstance] POST:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [DearLoadingView hideHUD];
        if (responseObject) {
            NSDictionary *dict = (NSDictionary *)responseObject;
            if ([dict[@"Result"] integerValue] == 0)
            {
                //密码修改成功
                [DearLoadingView showTips:dict[@"Message"]];
                //清空信息
                self.otherTextFiled.text = @"";
                self.againTextField.text = @"";
                self.oldTextFiled.text = @"";
                [self keyboardHidden];
            }else {
                [DearLoadingView showTips:dict[@"Message"]];
            }
        }else{
            [DearLoadingView showTips:@"登录失败"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [DearLoadingView hideHUD];
        [DearLoadingView showTips:error];
    }];
    

}

#pragma mark -文本框聚焦的时候
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == self.againTextField ) {
        [self moveToUp];
    }
}
#pragma mark - 向上移动
- (void)moveToUp{

    CGFloat offsetHeight = ScreenSize.height - 64 - (KeyBoard + TotalHeight);
    if (offsetHeight < 0) {
        //键盘要向上弹起
        [UIView animateWithDuration:0.25 animations:^{
            CGRect bgFrame = _bgView.frame;
            bgFrame.origin.y = offsetHeight;
            _bgView.frame = bgFrame;
        }];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    //回到原始位置
    if (textField == self.againTextField) {
       [UIView animateWithDuration:0.25 animations:^{
           CGRect bgFrame = _bgView.frame;
           bgFrame.origin.y = 0;
           _bgView.frame = bgFrame;;
       }];
    }
   
}

#pragma mark - 收键盘
- (void)keyboardHidden
{
    [self.oldTextFiled resignFirstResponder];
    [self.otherTextFiled resignFirstResponder];
    [self.againTextField resignFirstResponder];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
